package com.czl.module_storehouse.adapter;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.czl.base.data.bean.tengyun.RecordRemandBean;
import com.czl.module_storehouse.R;

import java.util.ArrayList;
import java.util.List;

/**
 * @author xubin
 */
public class RemandSelectedOrderAdapter extends BaseNumAdapter<RecordRemandBean> {

    private List<RecordRemandBean> mSelectedList = new ArrayList<>();
    private OnNumTextChangeListener mOnNumTextChangeListener;
    private int mTouchItemPosition = -1;

    public RemandSelectedOrderAdapter(int layoutResId, @Nullable List<RecordRemandBean> data) {
        super(layoutResId, data);
    }

    public void setOnNumTextChangeListener(OnNumTextChangeListener onNumTextChangeListener) {
        mOnNumTextChangeListener = onNumTextChangeListener;
    }

    public List<RecordRemandBean> getSelectedList() {
        return mSelectedList;
    }

    public void setSelectedList(List<RecordRemandBean> selectedList) {
        mSelectedList = selectedList;
    }

    @Override
    protected void convert(@NonNull BaseViewHolder helper, RecordRemandBean item) {
        item.setCount(item.getReturnNumInt());
        super.convert(helper, item);
        helper.setText(R.id.tv_name, item.getRecordTitle())
                .setText(R.id.tv_user, item.getRemandUser())
                .setText(R.id.tv_return_num, "待还：" + item.getNeedReturnNum())
                .setVisible(R.id.imageView, item.isRemandVisable())
                .setText(R.id.tv_bor_rec_num, item.getBorRecNum())
                .setText(R.id.tv_damage_num, "损坏：" + item.getExcNumInt());
        helper.getView(R.id.tv_damage_num).setOnClickListener(v -> {
            if (getOnItemChildClickListener() != null) {
                getOnItemChildClickListener().onItemChildClick(RemandSelectedOrderAdapter.this, v, helper.getAbsoluteAdapterPosition());
            }
        });
    }

    /*@Override
    protected void convertPayloads(@NonNull BaseViewHolder helper, RecordRemandBean item, @NonNull List<Object> payloads) {
        super.convertPayloads(helper, item, payloads);
        helper.setText(R.id.tv_damage_num, "损坏：" + item.getExcNumInt());
    }*/

    @Override
    public int getMaxCount(RecordRemandBean item) {
        return item.getNeedReturnNumInt() - item.getExcNumInt();
    }

    @Override
    public int getMinCount(RecordRemandBean item) {
        return 0;
    }

    @Override
    protected void setAfterTextChanged(BaseViewHolder helper, RecordRemandBean bean, int count) {
        if (count > 0) {
            mSelectedList.add(bean);
        } else {
            mSelectedList.remove(bean);
        }
        bean.setReturnNum(count);
        helper.setVisible(R.id.imageView, bean.isRemandVisable());
        if (mOnNumTextChangeListener != null) {
            mOnNumTextChangeListener.textChange();
        }
    }

    public interface OnNumTextChangeListener {
        void textChange();
    }
}
